import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:sleep/help_style.dart';
import 'package:sleep/page/fatigue/fatigue.dart';
import 'package:sleep/page/sleep/sleep.dart';
import 'package:sleep/page/user.dart';
import 'package:sleep/provider/blue/blue_provider.dart';
import 'package:sleep/provider/db_provider.dart';
import 'package:sleep/provider/fagigue_state_provider.dart';
import 'package:sleep/provider/sleep_osash_state_provider.dart';
import 'package:sleep/provider/sleep_state_provider.dart';
import 'package:sleep/provider/turn_over_provider.dart';
import 'package:sleep/provider/wave_deatil_provider/pluse_detail_provider.dart';
import 'package:sleep/provider/wave_deatil_provider/sleep_detail_provider.dart';

class Home extends StatefulWidget {
  const Home({super.key});

  @override
  State<Home> createState() => _HomeState();
}

class _HomeState extends State<Home> {
  // //存放bottombar信息，避免编写过多重复ui代码
  var _index = 0;

  final PageController _indexController =
      PageController(initialPage: 0 //默认为0，可以不填写
          );

  final bars = const [
    BottomNavigationBarItem(
        icon: Icon(Icons.disabled_visible), label: 'Fatigue'),
    BottomNavigationBarItem(icon: Icon(Icons.hotel), label: 'Sleep')
  ];
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
          currentIndex: _index,
          backgroundColor: Color(0xff0f2433),
          selectedItemColor: HelpStyle.primarycolor,
          unselectedItemColor: HelpStyle.grayTitleColor,
          onTap: (index) {
            setState(() {
              _index = index;
              _indexController.jumpToPage(_index);
            });
          },
          items: bars),
      body: PageView(
        controller: _indexController,
        physics: const NeverScrollableScrollPhysics(),
        children: children(),
      ),
    );
  }

  List<Widget> children() {
    Consumer(
      builder: (context, ref, child) {
        ref.watch(plueBlueProvider);
        ref.watch(sleepBlueProvider);
        ref.watch(faigueBlueProvider);
        ref.watch(sleepDetailProvider);
        ref.watch(pulseDetailProvider);
        ref.watch(fatigueStateProvider);
        ref.watch(turnOverProvider);
        ref.watch(sleeptateProvider);
        ref.watch(sleepOsashProvider);

        return const SizedBox(
          width: 0,
          height: 0,
        );
      },
    );
    return [
      Consumer(builder: (
        BuildContext context,
        WidgetRef ref,
        Widget? child,
      ) {
        final db = ref.watch(dbProvider);
        final sleep = db.getPerson(kFatiguePerson);

        return sleep == null
            ? UserPage(type: UserType.fatigue)
            : const Fatigue();
      }),
      Consumer(builder: (
        BuildContext context,
        WidgetRef ref,
        Widget? child,
      ) {
        final db = ref.watch(dbProvider);
        final sleep = db.getPerson(kSleepPerson);
        return sleep == null ? UserPage(type: UserType.sleep) : Sleep();
      }),
    ];
  }
}
